package parser;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import primitive.Article;

public class ArticleParser {
	private String stop_words_file = "files/common-english-words.txt";
	private List<String> stop_words;
	
	public Article remove_all(Article article) {
		Article art = remove_special_chars(article);
		art = remove_upper_case(art);
		art = remove_stop_words(art);

		return art;
	}

	public Article remove_stop_words(Article article) {
		if (stop_words == null) {
			read_stop_words();
		}
// <========================================================= begin of mess, fixit later		
		Article parset_article = new Article(article);		// Copy data from original object
		
		String body = article.getBody();
		
		String[] body_words = article.getBody().split(" ");
		body = "";
		
		//for (int i = 0; i < stop_words.size(); ++i) {
		//	body = body.replace(stop_words.get(i) + " ", " ");
		//}
		
		for (int i = 0; i < body_words.length; ++i)
		{
			String word = body_words[i];
			if (!stop_words.contains(word)) {
				body += word + " ";
			}
		}
// <============================================================ end of mess, fixit later
		parset_article.setBody(body);
		return parset_article;
	}
	
	public Article remove_special_chars(Article article) {
		Article pure_article = new Article(article);
		
		// Remove all comas, dots, etc
		String body = article.getBody();
		body = body.replaceAll("'s ", " ").replaceAll("ed ", " ");
		body = body.replaceAll("\\. ", " ").replaceAll(",", "");
		body = body.replaceAll("  ", " ");
		
		pure_article.setBody(body);
		return pure_article;
	}
	
	public Article remove_upper_case(Article article) {
		Article lower_article = new Article(article);
		
		String body = article.getBody();
		body = body.toLowerCase();
		
		lower_article.setBody(body);
		return lower_article;
	}
	
	// helpers
	
	private void read_stop_words() {
		stop_words = new ArrayList<>();
		
		try {
			FileInputStream fstream = new FileInputStream(stop_words_file);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			
			String current_line = "";
			
			while ((current_line = br.readLine()) != null) {
				String[] words = current_line.split(",");
				
				for (int i = 0; i < words.length; ++i) {
					stop_words.add(words[i]);
				}
			}
		} catch (Exception e) {
			//
		}
	}
}
